﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace hse_2013_test_dima_sasha
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            label6.Text += " "+Data.ListOfQuestions.Count().ToString();
        }

        private Form1 f1 = new Form1();
        private Results results;
        private  List<RadioButton> RbList = new List<RadioButton>();
        private List<Question> NewVariant = new List<Question>();

        public void ChangeSubject(string Subject)
        {
            label1.Text = Subject;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Рандомно Формируется вариант с вопросами
            Question question = new Question();
           NewVariant = question.SortQuestionsAndAnswers(int.Parse((string)comboBox1.SelectedItem));
        }

        private void DisplayAnswers(Answers[] answers)
        {
            //Выводит варианты ответов
            for (int i = 0; i < answers.Count(); i++)
            {
                RbList.ElementAt(i).Text = answers[i].GetAnswer();
            }
        }
        private void DisplayWrongAnswers(Answers[] answers)
        {
            //Выводит ошибки
            DisplayAnswers(answers);
            for (int i = 0; i < answers.Length; i++)
            {
                RbList.ElementAt(i).Checked = false;
                if (answers[i].IsCorrect)
                    RbList.ElementAt(i).ForeColor = Color.LawnGreen;
                else
                {
                    foreach (var Rb in RbList)
                    {
                        if (Rb.Text == Data.NewTrainee.GetWrongAnswers().ElementAt(click - 1).GetAnswer())
                        {
                            Rb.ForeColor = Color.Crimson;
                            break;
                        }
                    }
                }
            }
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            //Кнопка "Старт"
            if (textBox1.Text == "")
            {
                MessageBox.Show("Введите Своё Имя и Фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                textBox1.Enabled = false;
                Data.NewTrainee.SetName(textBox1.Text);
                RbList.Add(radioButton1);
                RbList.Add(radioButton2);
                RbList.Add(radioButton3);
                RbList.Add(radioButton4);
                RbList.Add(radioButton5);
                label6.Visible = false;
                button5.Visible = false;

                panel1.Visible = true;
                label2.Text = "Вопрос №1 ";
                label3.Text = NewVariant.ElementAt(0).GetVopros();
                DisplayAnswers(NewVariant.ElementAt(0).GetAnswers());
                foreach (var Rb in RbList)
                {
                    if (Rb.Text == " ")
                        Rb.Visible = false;
                }
                button3.Hide();
                comboBox1.Hide();
                label4.Hide();
                button1.Enabled = false;
            }
        }

        private void AddTraineeInfo()
        {
            //Метод сохраняет информацию о прохождении теста 
            for (int i = 0; i < RbList.Count; i++)
            {
                if (RbList.ElementAt(i).Checked)
                {
                    if (NewVariant.ElementAt(click - 1).GetAnswers()[i].IsCorrect)
                        Data.NewTrainee.SetScore(1);
                    else
                    {
                        Data.NewTrainee.SetWrong(NewVariant.ElementAt(click - 1));
                        Data.NewTrainee.SetWrongAnswer(NewVariant.ElementAt(click - 1).GetAnswers()[i].GetAnswer());
                    }
                    break;
                }
            }
        }
        
        private  int click = 1;//Счетчик. Нужен для последовательного отображения вопросов
    
        private void button4_Click(object sender, EventArgs e)
        {
            //Кнопка "Далее" или "->"
            button1.Enabled = true;
            click++;
            foreach (var Rb in RbList)
            {
                Rb.Visible = true;
                Rb.Text = String.Empty;
                Rb.ForeColor = Color.Black;
            }
            if (button4.Text == "Далее")
            {
                AddTraineeInfo();
                if (click == NewVariant.Count)
                    button4.Enabled = false;
                DisplayAnswers(NewVariant.ElementAt(click - 1).GetAnswers());
                label2.Text = "Вопрос № " + (click).ToString();
                label3.Text = NewVariant.ElementAt(click - 1).GetVopros();
            }
            else
            {
                if (click == Data.NewTrainee.GetWrong().Count)
                    button4.Enabled = false;
                label3.Text = Data.NewTrainee.GetWrong().ElementAt(click - 1).GetVopros();
                DisplayWrongAnswers(Data.NewTrainee.GetWrong().ElementAt(click - 1).GetAnswers());
            }
           
            foreach (var Rb in RbList)
            {
                if (Rb.Text == String.Empty)
                    Rb.Visible = false;
            }
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            //Кнопка "Завершить"
                if (click != NewVariant.Count)
                {
                    StreamWriter sw = new StreamWriter("name.txt", false, Encoding.Default);
                    sw.WriteLine(textBox1.Text);
                    sw.Close();
                    if (MessageBox.Show("Тест не закончен!\n Вы действительно хотите его завершить?", "Тест не закончен!",
                                        MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) ==
                        DialogResult.Yes)
                    {
                        for (int i = click - 1; i < NewVariant.Count; i++)
                        {
                            Data.NewTrainee.SetWrong(NewVariant.ElementAt(i));
                            Data.NewTrainee.SetWrongAnswer("Нет ответа");
                        }
                        results = new Results(this);
                        results.ShowDialog();
                    }
                }
                else
                {
                   /* StreamWriter sw = new StreamWriter("name.txt", false, Encoding.Default);
                    sw.WriteLine(textBox1.Text);
                    sw.Close();*/
                    AddTraineeInfo();
                    results = new Results(this);
                    results.ShowDialog();
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Кнопка "Назад" или "<-"
            click--;
            button4.Enabled = true;
                foreach (var Rb in RbList)
                {
                    Rb.Visible = true;
                    Rb.Text = String.Empty;
                    Rb.ForeColor = Color.Black;
                }
                if (button1.Text == "Назад")
                {
                    label2.Text = "Вопрос № " + (click).ToString();
                    label3.Text = NewVariant.ElementAt(click - 1).GetVopros();
                    DisplayAnswers(NewVariant.ElementAt(click - 1).GetAnswers());
                    Data.NewTrainee.RemoveMistake(Data.NewTrainee.GetWrong().Count-1);
                }
                else
                {
                    label3.Text = Data.NewTrainee.GetWrong().ElementAt(click - 1).GetVopros();
                    DisplayWrongAnswers(Data.NewTrainee.GetWrong().ElementAt(click - 1).GetAnswers());
                }
                foreach (var Rb in RbList)
                {
                    if (Rb.Text == String.Empty)
                        Rb.Visible = false;
                }
            if (click == 1)
                button1.Enabled = false;
        }
        /// <summary>
        /// Выводит ошибки на форму
        /// </summary>
        private void ShowMistakes()
        {
            button1.Enabled = false;
            button4.Enabled = true;
            button2.Enabled = false;
            button4.Text = "-->";
            button1.Text = "<--";
            label2.Text = "Ошибки: ";
            click = 1;
            if (click == Data.NewTrainee.GetWrong().Count)
                button4.Enabled = false;

            foreach (var Rb in RbList)
            {
                Rb.Visible = true;
                Rb.Text = String.Empty;
            }
            label3.Text = Data.NewTrainee.GetWrong().ElementAt(click-1).GetVopros();
            DisplayWrongAnswers(Data.NewTrainee.GetWrong().ElementAt(click - 1).GetAnswers());
            foreach (var Rb in RbList)
            {
                if (Rb.Text == String.Empty)
                    Rb.Visible = false;
            }
        }
        public void DoShowMistakes()
        {
            ShowMistakes();
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            f1.Show();
            Data.ListOfQuestions.Clear();
            Data.NewTrainee.RemoveTrainee();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsLetter(e.KeyChar))
                e.Handled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Рандомно Формируется вариант с вопросами
            Question question = new Question();
            NewVariant = question.SortQuestionsAndAnswers(Data.ListOfQuestions.Count);
            button3_Click(null, null);
        }
  
    }
}
